package com.estate.system.service.message;

import com.sendgrid.*;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class AdvancedEmailSender {

    private static final Logger log = LoggerFactory.getLogger(AdvancedEmailSender.class);

    private static final String SENDGRID_API_KEY = "pit-0ad069a4-e7f4-4a69-b192-ac489355420a";

    public static void main(String[] args) {
        // 示例用法
        EmailRequest emailRequest = new EmailRequest();
        emailRequest.setTo("it@mcconnellbourn.com.au");
        emailRequest.setFrom("it@mcconnellbourn.com.au");
        emailRequest.setSubject("Test");
        emailRequest.setContent("test");

        sendEmail(emailRequest);
    }

    public void send(String toEmail, String fromEmail, String subject, String content) {
        // 示例用法
        EmailRequest emailRequest = new EmailRequest();
        emailRequest.setTo(toEmail);
        emailRequest.setFrom(fromEmail);
        emailRequest.setSubject(subject);
        emailRequest.setContent(content);

        sendEmail(emailRequest);
    }

    public static void sendEmail(EmailRequest request) {
        SendGrid sendGrid = new SendGrid(SENDGRID_API_KEY);

        Email from = new Email(request.getFrom());
        Email to = new Email(request.getTo());
        Content content = new Content("text/plain", request.getContent());
        Mail mail = new Mail(from, request.getSubject(), to, content);

        Request sendGridRequest = new Request();
        try {
            sendGridRequest.setMethod(Method.POST);
            sendGridRequest.setEndpoint("emails/send");
            sendGridRequest.setBody(mail.build());

            Response response = sendGrid.api(sendGridRequest);

            log.info("=== 邮件发送结果 ===");
            log.info("状态码: " + response.getStatusCode());
            log.info("响应: " + response.getBody());

            if (response.getStatusCode() == 202) {
                log.error("邮件已成功接受并排队发送");
            } else {
                log.error("邮件发送失败");
                handleErrorResponse(response.getStatusCode(), response.getBody());
            }

        } catch (IOException e) {
            log.error("网络错误: " + e.getMessage());
        }
    }

    private static void handleErrorResponse(int statusCode, String body) {
        switch (statusCode) {
            case 400:
                log.error("错误: 请求格式错误");
                break;
            case 401:
                log.error("错误: API密钥无效");
                break;
            case 403:
                log.error("错误: 权限不足");
                break;
            case 413:
                log.error("错误: 请求体过大");
                break;
            case 429:
                log.error("错误: 请求频率限制");
                break;
            case 500:
                log.error("错误: SendGrid服务器内部错误");
                break;
            case 503:
                log.error("错误: 服务暂时不可用");
                break;
            default:
                log.error("错误: 未知错误，状态码: " + statusCode);
        }
        log.error("详细错误信息: " + body);
    }

    // 邮件请求数据类
    static class EmailRequest {
        private String to;
        private String from;
        private String subject;
        private String content;

        // getters and setters
        public String getTo() { return to; }
        public void setTo(String to) { this.to = to; }

        public String getFrom() { return from; }
        public void setFrom(String from) { this.from = from; }

        public String getSubject() { return subject; }
        public void setSubject(String subject) { this.subject = subject; }

        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
    }
}